package com.administrator.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.entity.Product;
import com.util.BaseDao;

/**
 * ProductDao
 *  LQM 对数据库表中的数据进行增删改查、原子性操作
 *
 */
public class ProductDao {
	//包括查、删、改、

	/*
	 * 查询所有商品方法
	 */
	public List<Product> findAllProduct(){
		try{
			  List<Product> list=new ArrayList<Product>(); 
			  PreparedStatement ps=null;
			  Connection conn=BaseDao.getConnection();
			  ps=conn.prepareStatement("select * from product");
			  ResultSet rs=ps.executeQuery();
			  Product p=null;
			  while(rs.next()){
				  p=new Product();
				  		p.setId(rs.getInt(1));
				  		p.setName(rs.getString(2));
				  		p.setPrice(rs.getInt(3));
				  		list.add(p);
				  		System.out.println(p);
			  		}
			  		return list; 
			} catch (SQLException e) {
					e.printStackTrace();
					return null;
			}		
	}

		/*
		 * 删除商品方法<根据id>
		 */
		public boolean deleteProductByid(int id){
			try{
				Connection con=BaseDao.getConnection();
				PreparedStatement pstm=con.prepareStatement("delete from product where id=?");
				pstm.setInt(1, id);
				int rs=pstm.executeUpdate();
				if(rs>0){
					return true;
				}else{
					return false;
				}
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
		}
		/*
		 * 修改商品方法
		 */
		public boolean updateProduct(Product product){
			try{
				Connection con=BaseDao.getConnection();
				PreparedStatement pstm=con.prepareStatement("update product set name=?,price=? where id=?");
				pstm.setString(1, product.getName());
				pstm.setInt(2, product.getPrice());
	            pstm.setInt(3, product.getId());
	            int rs = pstm.executeUpdate();
	            System.out.println(rs);
	            if(rs>0){
					return true;
				}else{
					return false;
				}
			}catch(Exception e){
				e.printStackTrace();
				return false;
			}
		}
		//
		public boolean isExist(int id) {
			// TODO Auto-generated method stub
			return false;
		}


}
